package logic;

import java.io.IOException;
import com.csvreader.CsvReader;

import model.EnergyProfile;
import model.SimpleEnergyProfile;

public class EnergyProfilerJava {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("EnergyProfiler");
		if (args.length!=5) {
			System.out.println("Input: Industry, UsageType, sqft area, AnnualEnergy(in MWh?), path of EnergyProfile file");
			System.out.print("Industry: ");
			System.out.print("[c]"+EnergyProfile.INDUSTRY[EnergyProfile.COMMERCIAL]+ " ");
			System.out.print("[i]"+EnergyProfile.INDUSTRY[EnergyProfile.INDUSTRIAL]+ " ");
			System.out.print("[r]"+EnergyProfile.INDUSTRY[EnergyProfile.RESIDENTIAL]+ " \n");
			
			System.out.println(EnergyProfile.INDUSTRY[EnergyProfile.COMMERCIAL]+" Usage Types: ");
			for (int i=0;i<DataInitializer.USAGETYPE_COMMERCIAL.length;i++) {
				System.out.println("["+i+"] " +DataInitializer.USAGETYPE_COMMERCIAL[i]);
			}
			
			System.out.println(EnergyProfile.INDUSTRY[EnergyProfile.INDUSTRIAL]+" Usage Types: ");
			for (int i=0;i<DataInitializer.USAGETYPE_INDUSTRIAL.length;i++) {
				System.out.println("["+i+"] " +DataInitializer.USAGETYPE_INDUSTRIAL[i]);
			}
			
			System.out.println(EnergyProfile.INDUSTRY[EnergyProfile.RESIDENTIAL]+" Usage Types: ");
			for (int i=0;i<DataInitializer.USAGETYPE_RESIDENTIAL.length;i++) {
				System.out.println("["+i+"] " +DataInitializer.USAGETYPE_RESIDENTIAL[i]);
			}
		}
		else {
			String industry ="";
			if ("c".equals(args[0])){
				industry = EnergyProfile.INDUSTRY[EnergyProfile.COMMERCIAL];
			}
			else if ("i".equals(args[0])){
				industry = EnergyProfile.INDUSTRY[EnergyProfile.INDUSTRIAL];
			}
			else if ("r".equals(args[0])){
				industry = EnergyProfile.INDUSTRY[EnergyProfile.INDUSTRIAL];
			}
			else {
				System.out.println("Invalid industry selected.");
				return;
			}

			System.out.println(industry+" industry selected.");
			String usageType = "";
			try {
				int usageTypeInt = Integer.parseInt(args[1]);
				if (usageTypeInt>=0) {
					if (industry.equals(EnergyProfile.INDUSTRY[EnergyProfile.COMMERCIAL]) 
							&& usageTypeInt<DataInitializer.USAGETYPE_COMMERCIAL.length) {
						usageType = DataInitializer.USAGETYPE_COMMERCIAL[usageTypeInt];
					}
					else if (industry.equals(EnergyProfile.INDUSTRY[EnergyProfile.INDUSTRIAL])
							&& usageTypeInt<DataInitializer.USAGETYPE_INDUSTRIAL.length) {
						usageType = DataInitializer.USAGETYPE_INDUSTRIAL[usageTypeInt];
					}
					else if (industry.equals(EnergyProfile.INDUSTRY[EnergyProfile.RESIDENTIAL])
							&& usageTypeInt<DataInitializer.USAGETYPE_RESIDENTIAL.length) {
						usageType = DataInitializer.USAGETYPE_RESIDENTIAL[usageTypeInt];
					}
					else {
						System.out.println("Invalid usage type");
						return;
					}
				}
				else {
					System.out.println("Invalid usage type");
					return;
				}
			}
			catch (NumberFormatException e) {
				System.out.println("Invalid usage type");
				return;
			}
			
			System.out.println(usageType+" usage type selected.");

			int areasqft=0;
			try {
				areasqft = Integer.parseInt(args[2]);
			}catch (NumberFormatException e) {
				System.out.println("Invalid sqftarea");
				return;
			}
			System.out.println(areasqft+" sqft area.");
			
			
			int annualEnergy=0;
			try {
				annualEnergy = Integer.parseInt(args[3]);
			}catch (NumberFormatException e) {
				System.out.println("Invalid annualEnergy");
				return;
			}
			System.out.println(annualEnergy+" annual energy.");
			
			String filename = args[4];
			SimpleEnergyProfile simpleEnergyProfile;
			try {
				simpleEnergyProfile = processInputCsv(filename);
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("Error opening file");
				return;
			}
			System.out.println(filename+" filename.");
			
			simpleEnergyProfile.setAnnualEnergy(annualEnergy);
			simpleEnergyProfile.setAreasqft(areasqft);
			simpleEnergyProfile.setIndustry(industry);
			simpleEnergyProfile.setUsageType(usageType);
			
			System.out.println(simpleEnergyProfile);
			
		}
	}
	
	private static SimpleEnergyProfile processInputCsv(String source) throws IOException {
		SimpleEnergyProfile simpleEnergyProfile = new SimpleEnergyProfile();
		System.out.println(System.getProperty("user.dir"));
		CsvReader reader = new CsvReader(source);
		 
		reader.readRecord();
		double[] doublearr = new double[24];
		String[] strarr = reader.getValues();
		for (int i=0;i<strarr.length;i++)
			doublearr[i]=Double.valueOf(strarr[i]);
		simpleEnergyProfile.setEnergyProfile(doublearr);
		return simpleEnergyProfile;
	}

}
